{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### leecode easy 通过在 60%\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "52"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2160. 拆分数位后四位数字的最小和\n",
    "# 扩展 不限制位数\n",
    "# 根据拆分后的数组进行切割\n",
    "num = 2932\n",
    "\n",
    "\n",
    "def minimumSum(num: int) -> int:\n",
    "    num = [int(n) for n in sorted(str(num))]\n",
    "    return (num[0] + num[1]) * 10 + num[2] + num[3]\n",
    "\n",
    "\n",
    "minimumSum(2392)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 771. 宝石与石头\n",
    "def numJewelsInStones(jewels: str, stones: str) -> int:\n",
    "    return sum([1 if s in jewels else 0 for s in stones])\n",
    "\n",
    "\n",
    "numJewelsInStones(\"aA\", \"aAAbbbb\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1, 2, 1, 1, 0, 0, 0, 0]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2006. 差的绝对值为 K 的数对数目\n",
    "def countKDifference(nums: list[int], k: int) -> int:\n",
    "    return [\n",
    "        nums[i + 1:].count(nums[i] - k) + nums[i + 1:].count(nums[i] + k)\n",
    "        for i in range(0,\n",
    "                       len(nums) - 1)\n",
    "    ]\n",
    "\n",
    "\n",
    "countKDifference([7, 7, 8, 3, 1, 2, 7, 2, 9, 5], 6)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2194. Excel 表中某个范围内的单元格\n",
    "def cellsInRange(s: str) -> list[str]:\n",
    "    return [\n",
    "        chr(i) + str(j) for i in range(ord(s[0]),\n",
    "                                       ord(s[3]) + 1)\n",
    "        for j in range(int(s[1]),\n",
    "                       int(s[4]) + 1)\n",
    "    ]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "import string\n",
    "\n",
    "\n",
    "def cellsInRange_2(s: str) -> list[str]:\n",
    "    return [\n",
    "        c + r for c in string.ascii_uppercase[ord(s[0]) - ord('A'):ord(s[3]) -\n",
    "                                              ord('A') + 1]\n",
    "        for r in '123456789'[ord(s[1]) - ord('1'):ord(s[4]) - ord('1') + 1]\n",
    "    ]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['K1', 'K2', 'K3', 'L1', 'L2', 'L3']"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cellsInRange(\"K1:L3\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['K1', 'K2', 'K3', 'L1', 'L2', 'L3']"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cellsInRange_2(\"K1:L3\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1512. 好数对的数目\n",
    "def numIdenticalPairs(nums: list[int]) -> int:\n",
    "    return len([(i, j) for i in range(len(nums)) for j in range(1, len(nums))\n",
    "                if nums[i] == nums[j] and i < j])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "import collections\n",
    "\n",
    "\n",
    "def numIdenticalPairs_2(nums: list[int]) -> int:\n",
    "    return sum(v * (v - 1) // 2 for _, v in collections.Counter(nums).items())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numIdenticalPairs_2([1, 2, 3, 1, 1, 3])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "def numIdenticalPairs_2(nums: list[int]) -> int:\n",
    "    ans = 0\n",
    "    temp = [0] * len(nums)\n",
    "    for n in nums:\n",
    "        ans += temp[n - 1]\n",
    "        temp[n - 1] += 1\n",
    "    return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numIdenticalPairs_2([1, 2, 3, 1, 1, 3])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 824. 山羊拉丁文\n",
    "def toGoatLatin(sentence: str) -> str:\n",
    "    return \"\".join([(s if s[0] in \"aeiouAEIUO\" else s[1:] + s[0]) + \"ma\" +\n",
    "                    \"a\" * (m + 1) + \" \"\n",
    "                    for m, s in enumerate(sentence.split())])[:-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa eiwmaaaaaaaa hetmaaaaaaaaa azylmaaaaaaaaaa ogdmaaaaaaaaaaa'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sentence = \"The quick brown fox jumped over wei the lazy dog\"\n",
    "toGoatLatin(sentence)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 733. 图像渲染\n",
    "class Solution:\n",
    "\n",
    "    def floodFillDFS(self, image: list[list[int]], sr: int, sc: int,\n",
    "                     newColor: int) -> list[list[int]]:\n",
    "        target = image[sr][sc]\n",
    "        dirs = [(-1, 0), (1, 0), (0, 1), (0, -1)]\n",
    "\n",
    "        def dfs(sr, sc):\n",
    "            if target != newColor:\n",
    "                if 0 <= sr <= len(image) - 1 and 0 <= sc <= len(\n",
    "                        image[0]) - 1 and image[sr][sc] == target:\n",
    "                    image[sr][sc] = newColor\n",
    "                    for dir in dirs:\n",
    "                        dfs(sr + dir[0], sc + dir[1])\n",
    "\n",
    "        dfs(sr, sc)\n",
    "        return image\n",
    "\n",
    "    def floodFillBFS(self, image: list[list[int]], sr: int, sc: int,\n",
    "                     newColor: int) -> list[list[int]]:\n",
    "        target = image[sr][sc]\n",
    "        if target == newColor:\n",
    "            return image\n",
    "        queue = [(sr, sc)]\n",
    "        dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]\n",
    "        while queue:\n",
    "            curx, cury = queue.pop(0)\n",
    "            image[curx][cury] = newColor\n",
    "            for dx, dy in dirs:\n",
    "                x, y = curx + dx, cury + dy\n",
    "                # 将同一优先级的加入队列等待\n",
    "                if 0 <= x <= len(image) - 1 and 0 <= y <= len(\n",
    "                        image[0]) - 1 and image[x][y] == target:\n",
    "                    queue.append((x, y))\n",
    "        return image\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[2, 2, 2], [2, 2, 0], [2, 0, 1]]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# test case\n",
    "image = [[1, 1, 1], [1, 1, 0], [1, 0, 1]]\n",
    "sr, sc, newColor = 1, 1, 2\n",
    "solution = Solution()\n",
    "solution.floodFillDFS(image, sr, sc, newColor)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[2, 2, 2], [2, 2, 0], [2, 0, 1]]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solution.floodFillBFS(image, sr, sc, newColor)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "###\n",
    "s = \"loveleetcode\"\n",
    "c = \"e\"\n",
    "[\n",
    "    min(abs(v - i) for i in [i for (i, v) in enumerate(s) if v == c])\n",
    "    for v in range(len(s))\n",
    "]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 819. 最常见的单词\n",
    "paragraph = \"Bob hit a ball, the hit BALL flew far after it was hit.\"\n",
    "banned = [\"hit\"]\n",
    "\n",
    "\n",
    "def mostCommonWord(paragraph: str, banned: list[str]) -> str:\n",
    "    import re\n",
    "    import collections\n",
    "    return max(\n",
    "        collections.Counter(re.split(r\"[ ,.!?';]\", paragraph.lower())).items(),\n",
    "        key=lambda x:\n",
    "        (len(x) > 0, x[0] not in b, x[1]))[0] if (b := set(banned +\n",
    "                                                           [\"\"])) else \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'b'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mostCommonWord(\"a, a, a, a, b,b,b,c, c\", [\"a\"])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "def mostCommonWord_2(paragraph: str, banned: list[str]) -> str:\n",
    "    import re\n",
    "    import collections\n",
    "    return collections.Counter(w for w in re.findall(r'\\w+', paragraph.lower()) if w not in set(banned)).most_common(1)[0][0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'b'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mostCommonWord_2(\"a, a, a, a, b,b,b,c, c\", [\"a\"])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2057. 值相等的最小索引\n",
    "def smallestEqual(nums: list[int]) -> int:\n",
    "    return next((i for i in range(len(nums)) if i % 10 == nums[i]), -1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "smallestEqual([0, 2, 3, 0, 1, 5])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2319. 判断矩阵是否是一个 X 矩阵\n",
    "\n",
    "def checkXMatrix(grid: list[list[int]]) -> bool:\n",
    "    return all((v != 0) == (i == j or i + j == len(grid) - 1) for i, row in enumerate(grid) for j, v in enumerate(row))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1030. 距离顺序排列矩阵单元格\n",
    "def allCellsDistOrder(R: int, C: int, r0: int, c0: int) -> list[list[int]]:\n",
    "    return sorted(((i, j) for i in range(R) for j in range(C)), key=lambda p: abs(p[0]-r0)+abs(p[1]-c0))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 905. 按奇偶排序数组\n",
    "def sortArrayByParity(nums: list[int]) -> list[int]:\n",
    "    return [v for v in nums if v % 2 == 0]+[v for v in nums if v % 2 == 1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 4, 1, 3]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sortArrayByParity([1,2,3,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 1935. 可以输入的最大单词数\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 剑指 Offer 17. 打印从1到最大的n位数 LCOF\n",
    "def printNumbers( n: int) -> list[int]:\n",
    "    return [x for x in range(1,10**n)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "printNumbers(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1,\n",
       " 2,\n",
       " 3,\n",
       " 4,\n",
       " 5,\n",
       " 6,\n",
       " 7,\n",
       " 8,\n",
       " 9,\n",
       " 10,\n",
       " 11,\n",
       " 12,\n",
       " 13,\n",
       " 14,\n",
       " 15,\n",
       " 16,\n",
       " 17,\n",
       " 18,\n",
       " 19,\n",
       " 20,\n",
       " 21,\n",
       " 22,\n",
       " 23,\n",
       " 24,\n",
       " 25,\n",
       " 26,\n",
       " 27,\n",
       " 28,\n",
       " 29,\n",
       " 30,\n",
       " 31,\n",
       " 32,\n",
       " 33,\n",
       " 34,\n",
       " 35,\n",
       " 36,\n",
       " 37,\n",
       " 38,\n",
       " 39,\n",
       " 40,\n",
       " 41,\n",
       " 42,\n",
       " 43,\n",
       " 44,\n",
       " 45,\n",
       " 46,\n",
       " 47,\n",
       " 48,\n",
       " 49,\n",
       " 50,\n",
       " 51,\n",
       " 52,\n",
       " 53,\n",
       " 54,\n",
       " 55,\n",
       " 56,\n",
       " 57,\n",
       " 58,\n",
       " 59,\n",
       " 60,\n",
       " 61,\n",
       " 62,\n",
       " 63,\n",
       " 64,\n",
       " 65,\n",
       " 66,\n",
       " 67,\n",
       " 68,\n",
       " 69,\n",
       " 70,\n",
       " 71,\n",
       " 72,\n",
       " 73,\n",
       " 74,\n",
       " 75,\n",
       " 76,\n",
       " 77,\n",
       " 78,\n",
       " 79,\n",
       " 80,\n",
       " 81,\n",
       " 82,\n",
       " 83,\n",
       " 84,\n",
       " 85,\n",
       " 86,\n",
       " 87,\n",
       " 88,\n",
       " 89,\n",
       " 90,\n",
       " 91,\n",
       " 92,\n",
       " 93,\n",
       " 94,\n",
       " 95,\n",
       " 96,\n",
       " 97,\n",
       " 98,\n",
       " 99]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "printNumbers(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 1374. 生成每种字符都是奇数个的字符串\n",
    "# n & 1\n",
    "def generateTheString( n: int) -> str:\n",
    "    return 'a'*n if n%2 else (n-1)*'a'+'b'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'aaaaaaaaab'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "generateTheString(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 1304. 和为零的 N 个不同整数\n",
    "# list(range(1 - n, n, 2))\n",
    "def sumZero( n: int) -> list[int]:\n",
    "    return [x for x in range(-(n-1)//2,(n-1)//2+1)] if n&1 else [x for x in range(-(n-1)//2,(n-1)//2+2) if x!=0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[-5, -4, -3, -2, -1, 1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sumZero(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 1413. 逐步求和得到正数的最小值\n",
    "def minStartValue( nums: list[int]) -> int:\n",
    "    import itertools\n",
    "    return max(-min(itertools.accumulate(nums)), 0) + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums = [-3,2,-3,4,2]\n",
    "minStartValue(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[-3, -1, -4, 0, 2]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import itertools\n",
    "list(itertools.accumulate(nums))\n",
    "class Solution:\n",
    "      # 空间复杂度为 O(m)，改用下标枚举可以达到 O(1)\n",
    "    def minDeletionSize(self, strs: list[str]) -> int:\n",
    "        import itertools\n",
    "        return sum(any(x > y for x, y in itertools.pairwise(col)) for col in zip(*strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 检查是否所有 A 都在 B 之前\n",
    "def checkString(s: str) -> bool:\n",
    "    # 全是b 或者以ab 分割完全不等\n",
    "    return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'bba']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = \"aabbba\"\n",
    "# s=\"aaabbb\"\n",
    "s.split('ab')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1897. 重新分配字符使所有字符串都相等\n",
    "\n",
    "def makeEqual(words: list[str]) -> bool:\n",
    "    import collections\n",
    "    d = set(collections.Counter(''.join(words)).values())\n",
    "    return len(words) == 1 or (len(d) == 1 and d[0] != 1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1616. 分割两个字符串得到回文串\n",
    "def checkPalindromeFormation(a: str, b: str) -> bool:\n",
    "    def isPalind(s:str)->bool:\n",
    "        return s==s[::-1]\n",
    "    if isPalind(a) or isPalind(b):\n",
    "        return True\n",
    "    else:\n",
    "        for i in range(1,len(a)):\n",
    "            s1,s2=a[0:i]+b[i::],b[0:i]+a[i::]\n",
    "            #print(i,s1,s2)\n",
    "            if isPalind(s1) or isPalind(s2):\n",
    "                return True\n",
    "    return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 uizalu jlacfd\n",
      "2 ulzalu jiacfd\n",
      "3 ulaalu jizcfd\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = \"ulacfd\"\n",
    "b = \"jizalu\"\n",
    "checkPalindromeFormation(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('a', 'bdef')"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s=\"abdef\"\n",
    "s[0:1],s[1::] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2535\n",
    "nums=[1,15,6,3,34,23,45]\n",
    "text=''.join([str(n) for n in nums if n>9])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "27"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum([int(t) for t in  text])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "90"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "abs(sum([n for n in nums if n>9])-sum([int(t) for t in  ''.join([str(n) for n in nums if n>9])]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2500\n",
    "def deleteGreatestValue( grid: list[list[int]]) -> int:\n",
    "    ans=0\n",
    "   \n",
    "    while grid:\n",
    "        max_i=0\n",
    "        for i in range(len(grid)): \n",
    "            max_i= max(grid[i]) if max_i<max(grid[i]) else max_i\n",
    "            #ans+=grid[i]\n",
    "\n",
    "        print(max_i)\n",
    "    return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 4]\n",
      "[3, 3, 1]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid = [[1,2,4],[3,3,1]]\n",
    "deleteGreatestValue(grid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum([v for i,v in enumerate(sorted(sum(grid,[]),reverse=True)) if i%2==0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 4\n",
      "1 3\n",
      "2 3\n",
      "3 2\n",
      "4 1\n",
      "5 1\n"
     ]
    }
   ],
   "source": [
    "for i,v in enumerate(sorted(sum(grid,[]),reverse=True)):\n",
    "    print(i,v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pivotInteger( n: int) -> int:\n",
    "    # x²=(n²+ n) / 2,\n",
    "    import math\n",
    "    x = round(math.sqrt((n * n + n) /2))\n",
    "    return x if x* x == (n * n + n) /2 else -1\n",
    "    # return [i if sum(range(1,i+1))== sum(range(i,n+1)) else 0 for i in range(1,n+1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[6]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pivotInteger(8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "def vowelStrings( words: list[str], left: int, right: int) -> int:\n",
    "    #return sum(words[i][0] in 'aeiou' and words[i][-1] in 'aeiou' for i in range(left,right+1))\n",
    "    return sum([1 if w[0] in ('a','o','e','i','u') and w[-1] in ('a','o','e','i','u') else 0 for w in words[left:right+1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "words = [\"are\",\"amy\",\"u\"]\n",
    "left = 0\n",
    "right = 2\n",
    "vowelStrings(words,left,right)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "def alternateDigitSum(n: int) -> int:\n",
    "    return sum([int(v) if i%2==0 else -1* int(v) for i,v in enumerate(str(n))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alternateDigitSum(521)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "def filterRestaurants(restaurants: list[list[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> list[int]:\n",
    "        ans=[]\n",
    "        for r in restaurants:\n",
    "            if veganFriendly:\n",
    "                if r[2] and r[3]<=maxPrice and r[4]<=maxDistance:\n",
    "                    ans.append((r[0],r[1]))\n",
    "            else:\n",
    "                if r[3]<=maxPrice and r[4]<=maxDistance:\n",
    "                    ans.append((r[0],r[1]))\n",
    "        # ans 排序,先按照第一个数 再按照第二个数\n",
    "\n",
    "        #sorted(ans,key=lambda x:(x[1],x[0]),reverse=True)\n",
    "        ans.sort(key=lambda x:(x[1],x[0]),reverse=True)\n",
    "        return [id for id,_ in ans]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 1, 5]\n"
     ]
    }
   ],
   "source": [
    "restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]]\n",
    "veganFriendly = 1\n",
    "maxPrice = 50\n",
    "maxDistance = 10\n",
    "print(filterRestaurants(restaurants, veganFriendly, maxPrice, maxDistance))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sumIndicesWithKSetBits( nums: list[int], k: int) -> int:\n",
    "    # 统计二进制1的个数\n",
    "    def countBits(n):\n",
    "        count=0\n",
    "        for r in range(n):\n",
    "            if r&1:\n",
    "                count+=1\n",
    "        return count\n",
    "\n",
    "    return sum([ num for index,num in enumerate(nums) if bin(index).count('1')== k ])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13\n"
     ]
    }
   ],
   "source": [
    "nums = [5,10,1,5,2]\n",
    "k = 1\n",
    "print(sumIndicesWithKSetBits(nums,k))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "def countBits(n):\n",
    "    count=0\n",
    "    while n:\n",
    "        n&=n-1\n",
    "        count+=1\n",
    "    return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "print(countBits(7))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sumIndicesWithKSetBits_2( nums: list[int], k: int) -> int:\n",
    "        return sum(x for i, x in enumerate(nums) if i.bit_count() == k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'int' object has no attribute 'bit_count'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m/var/folders/6z/ctcvj6ys70g5nx15m07t2cd80000gn/T/ipykernel_1984/388202733.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbit_count\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m: 'int' object has no attribute 'bit_count'"
     ]
    }
   ],
   "source": [
    "n=10\n",
    "n.bit_count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10000000001\n"
     ]
    }
   ],
   "source": [
    "def removeTrailingZeros( num: str) -> str:\n",
    "    # 去除字符串末尾的0\n",
    "    while num[-1]=='0':\n",
    "        num=num[:-1]\n",
    "    return num\n",
    "print(removeTrailingZeros('100000000010000000000000'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "75\n"
     ]
    }
   ],
   "source": [
    "test=\"7868190130M7522\"\n",
    "print(test[11:13])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rowAndMaximumOnes(self, mat: list[list[int]]) -> list[int]:\n",
    "    ans=[]\n",
    "    for index,row in enumerate(mat):\n",
    "        ans.append((index,row.count(1)))\n",
    "    ans.sort(key=lambda x:(x[1],-x[0]),reverse=True)\n",
    "    return list(ans[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fullBloomFlowers(flowers: list[list[int]], people: list[int]) -> list[int]:\n",
    "    # 差分数组\n",
    "    a_max=max(flowers,key=lambda x:x[1])[1]\n",
    "    flower_count=[0]*(a_max+2)\n",
    "    for flower in flowers:\n",
    "        flower_count[flower[0]]+=1\n",
    "        flower_count[flower[1]+1]-=1\n",
    "    for i in range(1,a_max+1):\n",
    "        flower_count[i]+=flower_count[i-1]\n",
    "    print(flower_count)\n",
    "    return [flower_count[i] for i in people]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 0, 1, 1, 0, 0, -1, -1, 1, 0, 0, 0, -1, -1]\n",
      "[0, 1, 1, 2, 3, 3, 3, 2, 1, 2, 2, 2, 2, 1, -1]\n",
      "[1, 2, 2, 2]\n"
     ]
    }
   ],
   "source": [
    "flowers = [[1,6],[3,7],[9,12],[4,13]]\n",
    "people = [2,3,7,11]\n",
    "print(fullBloomFlowers(flowers,people))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max(flowers,key=lambda x:x[1])[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "f0622d85ef30fc9193f22f988a126f7019729f34d340e6a1218ef75e650fe4af"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
